///
//	AssetLoaderExample v 1.7
//  Russell Lowke, August 23rd 2011
//
//	Copyright (c) 2009-2011 Russell Lowke
//	see http://www.lowkemedia.com for more information
//
//	Permission is hereby granted, free of charge, to any person obtaining a 
//	copy of this software and associated documentation files (the "Software"), 
//	to deal in the Software without restriction, including without limitation 
//	the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//	and/or sell copies of the Software, and to permit persons to whom the 
//	Software is furnished to do so, subject to the following conditions:
// 
//	The above copyright notice and this permission notice shall be included in 
//	all copies or substantial portions of the Software.
// 
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
//	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//	DEALINGS IN THE SOFTWARE. 
//
//
//	Note: You might need to set permissions for local file access from the 
//	Flash Global Security Settings panel, see,
//		http://www.macromedia.com/support/documentation/en/flashplayer/help/
//		and select "Global Security Settings Panel"
//
//  Note: The compiler setting for this project has been set to -use-network=false
//  This allows loading of local files but not loading over the web.


package 
{
	import com.lowke.assetLoader.AssetLoader;
	import com.lowke.assetLoader.event.LoaderProgressEvent;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.media.Sound;
	
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="30")]
	public class AssetLoaderExample extends Sprite
	{
		private var _assetLoader:AssetLoader;	// instance of AssetLoader
		
		private var _config:XML;				// XML
		private var _background:Bitmap;			// background Bitmap
		private var _sprite:Sprite;				// Sprite from .swf
		private var _ship:MovieClip;			// MovieClip from .swf
		private var _ufoClass:Class;			// Class definition from .swf
		private var _click:Sound;				// sounds
		private var _whistle:Sound;
		
		public function AssetLoaderExample()
		{
			// get a refernece to the AssetLoader singleton
			_assetLoader = AssetLoader.instance;
			
			// let's listen to the loader's progress
			_assetLoader.addEventListener(LoaderProgressEvent.PROGRESS, printProgress);
			
			// batch load some files
			_assetLoader.loadFile("config.xml", assign, "_config");
			_assetLoader.loadFile("assets/Blue-Planet-Earth.jpg", assign, "_background");
			_assetLoader.loadClass("assets/text.swf", "PlanetEarth", assign, "_sprite");
			_assetLoader.loadClass("assets/ship.swf", "Ship", assign, "_ship");
			_assetLoader.loadFile("assets/click.mp3", assign, "_click");
			_assetLoader.loadFile("assets/whistle.mp3", assign, "_whistle");
			_assetLoader.loadClassDefinition("assets/ufo.swf", "UFO", assign, "_ufoClass");
			
			// when batch finished loading call ready()
			_assetLoader.whenDone(ready);
		}
		
		// generic assign()
		private function assign(asset:*, memberVariable:String):void 
		{
			if (! asset) {
				// asset failed to load!
				throw new Error("Asset for " + memberVariable + " failed to load.");
			}
			
			// use associative syntax to assign asset to member variable
			this[memberVariable] = asset;
		}
		
		private function printProgress(event:LoaderProgressEvent):void {
			trace("Percent loaded:" + Math.round(event.percentDone*100) + "%");	
		}
		
		private function ready(event:Event = null):void
		{
			// rememeber to remove event listeners
			_assetLoader.removeEventListener(LoaderProgressEvent.PROGRESS, printProgress);

			//
			// your assets are loaded and ready
			
			// print the XML data
			trace("config XML:" + _config);			
			
			// display the background
			addChild(_background);
			
			// center and show "Planet Earth" sprite
			_sprite.x = (stage.width - _sprite.width)/2;	
			_sprite.y = 26;
			addChild(_sprite);							
			
			// show "Ship" MovieClip
			_ship.x = 100; _ship.y = 100;
			addChild(_ship);
			
			// add four to eight UFOs
			var nUFOs:uint = randomInt(4, 8);				
			for (var i:uint = 0; i < nUFOs; ++i) {
				
				// create an asteroid MovieClip from Class definition
				var ufo:MovieClip = new _ufoClass();
				
				// randomly place UFO
				ufo.x = randomInt(ufo.width, stage.width - ufo.width);
				ufo.y = randomInt(100, stage.height - ufo.height);
				addChild(ufo);
			}
			
			// play whistle sound
			_whistle.play();
		}
		
		//
		// generate a random integer from low value to high value
		public static function randomInt(low:int, high:int):int 
		{
			var range:int = (high + 1) - low;
			var rtn:int = Math.floor(Math.random() * range) + low;
		
			return rtn;
		}
	}
}